<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2017/12/21
 * Time: 13:47
 */

namespace Admin\Logic;
use Think\Model;

class EnquiryLogic extends Model
{
    protected $trueTableName = 'wz_enquiry';

    /**
     * @param $params
     * @return mixed
     */
    public function getEnquiryInfo($params){
        $info = D("Resource/Enquiry","Service")->getEnquiryInfo($params);
        if(isset($info) && $info){
            $info['enquiryDetail'] = D('Resource/EnquiryDetail','Service')->getAllEnquiryDetail(['eid'=>$info['id']]);
        }
        return $info;
    }
    /**
     * @param $params
     * @return string
     */
    public function saveEnquiry($params){
        $id = isset($params['id'])?intval($params['id']):'';
        $name = isset($params['name'])?strval($params['name']):'';
        $status = isset($params['status'])?intval($params['status']):'';
        $item_ids = isset($params['item_ids'])?strval($params['item_ids']):'';
        $detail = isset($params['detail'])?$params['detail']:[];
        if(!$item_ids){
           return Response(2004,"请选择产品");
        }

        if(!$id){
            $data=[];
            $data['name']= $name;
            $data['status']= $status;
            $data['item_ids'] = $item_ids;
            $eid = D('Resource/Enquiry','Service')->addEnquiry($data);
            if(!$eid){
               return Response(2001,'请求失败');
            }
            if(is_array($detail) && count($detail)>0){
                foreach ($detail as $v){
                    $arr = [];
                    $arr['eid'] = $eid;
                    $arr['title'] = $v['title'];
                    $arr['type'] = $v['type'];
                    $arr['data'] = $v['values'];
                    D('Resource/EnquiryDetail','Service')->addEnquiryDetail($arr);
                }
            }
            return Response(999,"请求成功");
        }else{
            $data=[];
            $data['id']= $id;
            $data['name']= $name;
            $data['status']= $status;
            $data['item_ids'] = $item_ids;
            $result = D('Resource/Enquiry','Service')->updateEnquiryInfo($data);
            if(!$result){
                return Response(2001,'请求失败');
            }
            if(is_array($detail) && count($detail)>0){
                foreach ($detail as $v){
                        if(!$v['id']){
                            $arr = [];
                            $arr['eid'] = $id;
                            $arr['title'] = $v['title'];
                            $arr['type'] = $v['type'];
                            $arr['data'] = $v['values'];
                            D('Resource/EnquiryDetail','Service')->addEnquiryDetail($arr);
                        }
                    }
            }
            return Response(999,"请求成功");
        }
    }

    /**
     * @param $params
     * @return string
     */
    public function deleteEnquiry($params){
        $id = $params['id'];
        $status = $params['status'];

        $data=[];
        $data['id']= $id;
        $data['status']= $status;

        $result = D('Resource/Enquiry','Service')->updateEnquiryInfo($data);
        if(!$result){
            return Response(2001,'请求失败');
        }
        return Response(999,'请求成功');
    }
}